/**
 * related to incomingrecord.ui
 * 
 * @Author : lily80239464
 * @Timestamp : 2017-11-13
 */
var do_App = sm("do_App")
var nf = sm("do_Notification");
var do_Page = sm("do_Page");
var do_DataCache = sm("do_DataCache");
var util = require("util");
var do_ALayout_search = ui("do_ALayout_searchImage");
var do_textField_phoneNum = ui("do_TextField_phoneNum");
util.init("do_ALayout_back");
var do_ListView_incomingRecord = ui("do_ListView_incomingrecord");
var do_ALayout_root = ui("do_ALayout_root");
do_ALayout_root.add("loadingUI", "source://view/loading/loadingUI.ui", 0, 0);
var loadingUI = ui("loadingUI");
loadingUI.visible =true;
var url;
//listview的数据源
var listDataRecord = mm("do_ListData");
do_ListView_incomingRecord.bindItems(listDataRecord);

do_Page.on("loaded",function(data, e) {
	var baseUrl = do_DataCache.loadData("baseUrl");
	url = baseUrl+"getphonehistory";
	refreshAllData();
});
//刷新数据
function refreshAllData() {
	var http = mm("do_Http");
	http.method="GET";
	http.contentType = "application/x-www-form-urlencoded";
	http.timeout=10000;
	http.url = url;
	http.on("success", function(data, e) {
		loadingUI.visible = false;
		var jsonString = data.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<string xmlns=\"http://tempuri.org/\">","");
		jsonString = jsonString.replace("</string>","");
		deviceone.print(jsonString);
		bean = JSON.parse(jsonString);
		if (bean.result ==200) {
			//恢复do_ListView_news的headerview和footerview状态
			do_ListView_incomingRecord.rebound();
			listDataRecord.removeAll();
			listDataRecord.addData(bean.returnObj);
			//刷新显示
		    do_ListView_incomingRecord.refreshItems();
			
		}else {
			nf.toast(bean.msg);
		}
		
	});
	http.on("fail",function(data, e) {
		loadingUI.visible = false;
		do_ListView_news.rebound();
		//do_Notification.toast(data);
		do_Notification.toast("网络故障"); 
	});
	http.request();
};
//下拉列表，刷新数据
do_ListView_incomingRecord.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});
var listDataSearch = mm("do_ListData");

do_ALayout_search.on("touch", function(data, e) {
	if (do_textField_phoneNum.text=="") {
		do_ListView_incomingRecord.bindItems(listDataRecord);
	}
	for (var int = 0; int < listDataRecord.getCount(); int++) {
		var phoneNum = listDataRecord.getOne(i).rscchnl;
		if (phoneNum.indexOf(do_textField_phoneNum.text)>0) {
			listDataSearch.addOne(listDataRecord.getOne(i));
		}
	}
	do_ListView_incomingRecord.bindItems(listDataSearch);
	do_ListView_incomingRecord.refreshItems();
});
